package vn.com.fsoft.action;

import java.util.List;

import vn.com.fsoft.business.impl.ContactBusiness;
import vn.com.fsoft.model.entities.Contact;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author VangND1
 * 
 */
public class ContactAction extends ActionSupport {

	/**
	 * This generate version.
	 */
	private static final long serialVersionUID = 5105091769691885885L;

	public ContactAction() {
		contactBusiness = new ContactBusiness();
	}

	public final int pageSize = 5;

	public String listAllContact() {
		contactBusiness = new ContactBusiness();
		/*
		 * beginPage = 0; maxRow = 5;
		 */
		currentPage = 1;
		long totalRow = contactBusiness.getRowCountContact();
		if (pageSize > totalRow) {
			currentPage = 0;
		} else {
			if (totalRow % pageSize == 0) {
				totalPage = totalRow / pageSize;
			} else {
				totalPage = (totalRow / pageSize) + 1;
			}
		}
		listContact = contactBusiness.getAllContact(pageSize, currentPage);
		return SUCCESS;
	}

	public String nextPage() {
		/*
		 * if (maxRow > 0) { beginPage = beginPage + 5; listContact =
		 * contactBusiness.getAllContact(beginPage, maxRow); maxRow =
		 * listContact.size(); }
		 */
		currentPage = currentPage + 1;
		if (currentPage > totalPage)
			currentPage = (int) totalPage;
		listContact = contactBusiness.getAllContact(pageSize, currentPage);
		return SUCCESS;
	}

	public String prevPage() {
		/*
		 * maxRow = 5; beginPage = beginPage - 5; if (beginPage < 0) beginPage =
		 * 0;
		 */
		currentPage = currentPage - 1;
		if (currentPage <= 0)
			currentPage = 1;
		listContact = contactBusiness.getAllContact(pageSize, currentPage);
		// listContact = contactBusiness.getAllContact(beginPage, maxRow);
		System.out.println(contactBusiness.getRowCountContact());
		return SUCCESS;
	}

	/* Declare variable */
	private ContactBusiness contactBusiness;
	private List<Contact> listContact;
	private static long totalPage;
	/*
	 * private int beginPage; private static int maxRow;
	 */
	private static int currentPage;

	/* Getter and Setter */

	public List<Contact> getListContact() {
		return listContact;
	}

	public ContactBusiness getContactBusiness() {
		return contactBusiness;
	}

	public void setContactBusiness(ContactBusiness contactBusiness) {
		this.contactBusiness = contactBusiness;
	}

	public void setListContact(List<Contact> listContact) {
		this.listContact = listContact;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		ContactAction.currentPage = currentPage;
	}

	public long getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(long totalPage) {
		ContactAction.totalPage = totalPage;
	}

	/*
	 * public int getMaxRow() { return maxRow; }
	 * 
	 * public void setMaxRow(int maxRow) { this.maxRow = maxRow; }
	 * 
	 * public int getBeginPage() { return beginPage; }
	 * 
	 * public void setBeginPage(int beginPage) { this.beginPage = beginPage; }
	 */

}
